package rd.android.hardcore.engine 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import rd.android.hardcore.params.GlobalParams;
	import rd.android.hardcore.ui.pad.Pad;
	/**
	 * ...
	 * @author Fazermokeur
	 */
	public class PageManager extends Sprite
	{
		
		private var a_Pads	: Vector.<Pad>;
		private var _isOnStage : Boolean = false;
		
		public function PageManager(data:Vector.<Pad> ) 
		{
			a_Pads = data;
			
			addEventListener(Event.ADDED_TO_STAGE, placeItem);
		}
		
		
		private function placeItem(e:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, placeItem);
			addEventListener(Event.REMOVED_FROM_STAGE, dispose);
			
			isOnStage = true;
			trace("placeItem");
			var line:int = 0;
			var hauteur:int = 40;
			var marginLeft : int = 15;
			var size:int;
			
			if (stage.deviceOrientation == "upsideDown" || stage.deviceOrientation == "default") {
				size = GlobalParams.PAD_SIZE;
			}else {
				size = GlobalParams.PAD_SIZE - 4;
			}
			
			var len:int = (a_Pads.length < 24) ? a_Pads.length : 24;
			var i:int = 0;
			
			for (var j:int = 0; j < len; j++)
			{
				addChild(a_Pads[j]);
				a_Pads[j].resize(size);
				a_Pads[j].x = marginLeft + (i % 4) * (size + 5);
				a_Pads[j].y = hauteur + (line % 6) * (size + 5);
				i++;
				if (i % 4 == 0) line++;
			}
		}
		
		private function dispose(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, dispose);
			addEventListener(Event.ADDED_TO_STAGE, placeItem);
			
			isOnStage = true;
			
			var len:int = (a_Pads.length < 24) ? a_Pads.length : 24;
			for (var j:int = 0; j < len; j++)
			{
				removeChild(a_Pads[j]);
			}
		}
		
		public function get isOnStage():Boolean { return _isOnStage; }
		
		public function set isOnStage(value:Boolean):void 
		{
			_isOnStage = value;
		}
	}

}